﻿Library.UserInterface.Widgets.Translate = function (options) {
    var defaults = {
        container: null,
        fromLang: "en",
        toLang: "tr",
        loader: "Yükleniyor...",
        titleSelector: "",
        onSettingsSave: ""        
    };
    var options = $.extend(defaults, options);

    this.container = options.container;
    this.fromLang = options.fromLang;
    this.toLang = options.toLang;
    this.loader = options.loader;
    this.titleSelector = options.titleSelector;
    this.onSettingsSave = options.onSettingsSave;

    this.initialize(false);
}

Library.UserInterface.Widgets.Translate.prototype = {
    container: null,
    fromLang: null,
    toLang: "",
    loader: "",
    titleSelector: "",
    onSettingsSave: "",
    fullscreen: false,
    minimize: true,
    settings: true,
    id: null,
    type: "widget.translate",

    initialize: function (set) {
        if (this.id != null)
            Library.UserInterface.Widgets.Dispose(this.id);
        
        this.id = Library.Util.ReplaceAll(Library.Crypto.Guid(), "-", "");
        this.container.find(".portlet-widget").html(this.loader);

        Library.UserInterface.Widgets.Add(this);

        if (this.titleSelector == "")
            this.titleSelector = this.container.find(".portlet-title").selector;

        if (this.titleSelector != "")
            $(this.titleSelector).html(this.loader);

        try {
            if (set) {
                this.container.find(".portlet-widget").toggle("blind", {}, 500);
                this.container.find(".portlet-widget").parents(".portlet-content").find(".portlet-settings").toggle("blind", {}, 500);
            }

            var widgetHtml = "";
            widgetHtml += "<table class=\"Forms\"><tr><td>";
            widgetHtml += "<textarea style=\"width:350px; height:50px\" class=\"Text ui-translate\" onkeyup=\"Library.UserInterface.Widgets.Get('" + this.id + "').translate($(this).val())\"></textarea>";
            widgetHtml += "</td></tr><tr><td>";
            widgetHtml += "<div class=\"ui-translation\" style=\"width:350px; height:50px\"></div>";
            widgetHtml += "</td></tr></table>";
            this.container.find(".portlet-widget").html(widgetHtml);

            if (this.titleSelector != "")
                $(this.titleSelector).html("Çeviri Aracı");

            this.getSettings();
            Library.UserInterface.Widgets.SetHeaderIcons(this);
        } catch (err) {
            Library.UserInterface.Widgets.Error(this.id, err); 
        }
    },

    getSettings: function () {
        var settingsHTML = "";
        try {
            settingsHTML += "<table class=\"Forms\"><tr>";
            settingsHTML += "<td>";
            settingsHTML += "<select class='ui-translate-fromlang' onchange=\"Library.UserInterface.Widgets.Get('" + this.id + "').translate($(this).parents('.portlet-content').find('.ui-translate').val())\">";
            settingsHTML += "</select>";
            settingsHTML += "</td>";
            settingsHTML += "<td class=\"Buttons\">";
            settingsHTML += "<button style=\"font-size:12px; width:30px\" onclick=\"Library.UserInterface.Widgets.Get('" + this.id + "').swap(); return false;\"><img src=\"/Themes/Standart/TranslateChange.gif\" /></button>";
            settingsHTML += "</td>";
            settingsHTML += "<td>";
            settingsHTML += "<select class='ui-translate-tolang' onchange=\"Library.UserInterface.Widgets.Get('" + this.id + "').translate($(this).parents('.portlet-content').find('.ui-translate').val())\">";
            settingsHTML += "</select>";
            settingsHTML += "</td>";
            settingsHTML += "<td><td class=\"Buttons\">";
            settingsHTML += "<input type=\"button\" value=\"Kaydet\" style=\"font-size:12px; width:60px\" onclick=\"Library.UserInterface.Widgets.Get('" + this.id + "').save()\"/>";
            settingsHTML += "</td></tr></table>";

            var widget = this;

            $.get("/Util/ParameterXml.aspx", { TableName: "PORTLET_TRANSLATE" },
            function (data) {
                data = $($(data)[1]);
                var fromLang = "";
                var toLang = "";
                var len = data.find("parametre").length;
                for (var xd = 0; xd < len; xd++) {
                    fromLang += "<option value=\"" + $(data.find("parametre")[xd]).find("kodu")[0].childNodes[0].data + "\" " + (widget.fromLang == $(data.find("parametre")[xd]).find("kodu")[0].childNodes[0].data ? "selected='selected'" : "") + ">" + $(data.find("parametre")[xd]).find("aciklama")[0].childNodes[0].data + "</option>";
                    toLang += "<option value=\"" + $(data.find("parametre")[xd]).find("kodu")[0].childNodes[0].data + "\" " + (widget.toLang == $(data.find("parametre")[xd]).find("kodu")[0].childNodes[0].data ? "selected='selected'" : "") + ">" + $(data.find("parametre")[xd]).find("aciklama")[0].childNodes[0].data + "</option>";
                }
                $(".ui-translate-fromlang").html(fromLang);
                $(".ui-translate-tolang").html(toLang);
            });
            this.container.find(".portlet-settings").html(settingsHTML);
        } catch (err) {
            Library.UserInterface.Widgets.Error(this.id, err); 
        }
    },

    save: function () {
        this.fromLang = this.container.find(".ui-translate-fromlang").val();
        this.toLang = this.container.find(".ui-translate-tolang").val();
        
        if (jQuery.isFunction(this.onSettingsSave))
            eval(this.onSettingsSave + "('" + fromLang + "-" + toLang + "')");
        
        this.initialize(true);
    },

    swap: function () {
        var fromLang = this.container.find(".ui-translate-fromlang");
        var toLang = this.container.find(".ui-translate-tolang");

        var tmp = toLang.val();
        toLang.val(fromLang.val());
        fromLang.val(tmp);

        this.translate(this.container.find(".ui-translate").val());
    },

    translate: function (text) {
        var widget = this;
        $.translate(text, this.container.find('.ui-translate-fromlang').val(), this.container.find('.ui-translate-tolang').val(), {
            complete: function () {
                widget.container.find(".ui-translation").html(this.translation);
            },
            error: function (err) {
                widget.container.find(".ui-translation").html(err.message);
                Library.UserInterface.Widgets.Error(this.id, err); 
            }
        });
    }
}